// TriButton.cpp : implementation file
//

#include "GridControl/StdAfx.h"
#include "TriButton.h"

#include "ARES.rc.h"


// CTriButton

IMPLEMENT_DYNAMIC(CTriButton, CButton)

CTriButton::CTriButton() : CButton()
{
	_VMESSAGE("Creating TriButton");
}

CTriButton::~CTriButton()
{
	_VMESSAGE("Destroying TriButton");
	CButton::~CButton();
}


BEGIN_MESSAGE_MAP(CTriButton, CButton)
END_MESSAGE_MAP()



// CTriButton message handlers




void CTriButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
	_DMESSAGE("Drawing TriButton");

	CDC dc;
	dc.Attach(lpDrawItemStruct->hDC);     //Get device context object
	int nWidth = GetSystemMetrics(SM_CXMENUCHECK);
	int nMargin = ( nWidth - 8 ) / 2;

	CRect textRt = lpDrawItemStruct->rcItem;
	textRt.right = textRt.right - nWidth - nMargin;

	CString text;
	GetWindowText(text);

	UINT textDrawState = DST_TEXT;
	if ( lpDrawItemStruct->itemState & ODS_DISABLED )
		textDrawState |= DSS_DISABLED;

	dc.DrawState(CPoint(textRt.left, textRt.top), textRt.Size(), text, textDrawState, TRUE, 0, (CBrush*)NULL);

	CRect rt = lpDrawItemStruct->rcItem;	// initial rect is for entire button
	rt.left = rt.right - nWidth;			// set left margin
	LONG center = ( rt.bottom + rt.top ) / 2;
	rt.top = center - nWidth/2;
	rt.bottom = center + nWidth/2;

	UINT checkDrawState = DFCS_BUTTONCHECK;
	if ( lpDrawItemStruct->itemState & ODS_DISABLED )
		checkDrawState |= DFCS_INACTIVE;

	if ( lpDrawItemStruct->itemState & ODS_CHECKED )
		checkDrawState |= DFCS_CHECKED;

	else if ( GetCheck() == BST_INDETERMINATE ) {
		_VMESSAGE("Indeterminate; custom draw.");

		CBitmap indet_check = CBitmap();
		indet_check.LoadBitmap(IDB_INDET_CHECK);

		CPoint pt = CPoint(rt.left + nMargin, rt.top + nMargin);
		CSize sz = CSize(8, 8);

		dc.DrawState(pt, sz, &indet_check, DST_BITMAP|DSS_NORMAL);
	}
	
	dc.DrawFrameControl(rt, DFC_BUTTON, checkDrawState);
}
